<?php

namespace app\common\model;

use think\Model;

/**
 * 商家端店铺成员权限
 */
class ShopMemberPermission extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [

    ];

    /**
     * 获取店员菜单权限
     * @param $member_id
     * @param $shop_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMemberPermission($member_id,$shop_id)
    {
        //查看角色
        $info = ShopMember::get(['shop_id'=>$shop_id,'member_id'=>$member_id]);
        if ($info['is_admin'] == 1){ //管理员
           return InterfacePermission::field('id,name,page_url,image')->order('sort desc')->select();
        }else{ //普通店员
            $permiss  = self::where(['member_id'=>$member_id,'shop_id'=>$shop_id])->group('permission_id')->column('permission_id');
            if ($permiss)
                return InterfacePermission::field('id,name,page_url,image')->whereIn('id',$permiss)->order('sort desc')->select();
            return [];
//
//            return self::alias('a')->field('b.name,b.page_url,b.image')
//                ->join('fa_interface_permission b','a.permission_id = b.id')
//                ->where(['a.member_id'=>$member_id,'a.shop_id'=>$shop_id])->order('b.sort desc')->select();
        }

    }

}
